function varargout = Trebfun_gui(varargin)
global turn;
global p2;
global height;
global started;
global numofplayers;
% TREBFUN_GUI M-file for Trebfun_gui.fig
%      TREBFUN_GUI, by itself, creates a new TREBFUN_GUI or raises the existing
%      singleton*.
%
%      H = TREBFUN_GUI returns the handle to a new TREBFUN_GUI or the handle to
%      the existing singleton*.
%
%      TREBFUN_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in TREBFUN_GUI.M with the given input arguments.
%
%      TREBFUN_GUI('Property','Value',...) creates a new TREBFUN_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Trebfun_gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Trebfun_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Trebfun_gui

% Last Modified by GUIDE v2.5 16-Dec-2009 19:11:20

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Trebfun_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @Trebfun_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Trebfun_gui is made visible.
function Trebfun_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Trebfun_gui (see VARARGIN)

% Choose default command line output for Trebfun_gui
handles.output = hObject;
global started;
started=0;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Trebfun_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Trebfun_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function M1_Callback(hObject, eventdata, handles)
% hObject    handle to M1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of M1 as text
%        str2double(get(hObject,'String')) returns contents of M1 as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','50')
end
guidata(hObject,handles)
    


% --- Executes during object creation, after setting all properties.
function M1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to M1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function M2_Callback(hObject, eventdata, handles)
% hObject    handle to M2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of M2 as text
%        str2double(get(hObject,'String')) returns contents of M2 as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','5')
end
guidata(hObject,handles)
    

% --- Executes during object creation, after setting all properties.
function M2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to M2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function L1_Callback(hObject, eventdata, handles)
% hObject    handle to L1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of L1 as text
%        str2double(get(hObject,'String')) returns contents of L1 as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','5')
end
guidata(hObject,handles)
    

% --- Executes during object creation, after setting all properties.
function L1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to L1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function R_Callback(hObject, eventdata, handles)
% hObject    handle to R (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of R as text
%        str2double(get(hObject,'String')) returns contents of R as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','5')
end
guidata(hObject,handles)
    

% --- Executes during object creation, after setting all properties.
function R_CreateFcn(hObject, eventdata, handles)
% hObject    handle to R (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function L2_Callback(hObject, eventdata, handles)
% hObject    handle to L2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of L2 as text
%        str2double(get(hObject,'String')) returns contents of L2 as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','1')
end
guidata(hObject,handles)
    

% --- Executes during object creation, after setting all properties.
function L2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to L2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function R2_Callback(hObject, eventdata, handles)
% hObject    handle to R2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of R2 as text
%        str2double(get(hObject,'String')) returns contents of R2 as a double
if isempty(str2double(get(hObject,'string')))
    set(hObject,'string','1')
end
guidata(hObject,handles)
    

% --- Executes during object creation, after setting all properties.
function R2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to R2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in Fire.
function Fire_Callback(hObject, eventdata, handles)
% hObject    handle to Fire (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global turn;
global height;
global p2;
global started;
global numofplayers;
if (isempty(started)||started==0)
    turn=round(rand*1.99999+.5)
    p2=25+rand*100;
    height=10+rand*15;
    started=1;
    set(hObject,'string','FIRE!')
else
   
if (turn==1)
    turn=2;
else
    turn=1;
end
end
 if (length(findobj('Type','figure'))>1)
    close('FUN!');
    end
figure('Name','FUN!','Position',[0,0,1000,800],'Color',[0 1 1])
theta = 200;
phi = 270;
psi = 45;
g = 9.8;
t = 30;
M1 = str2num(get(handles.M1,'string'));
M2 = str2num(get(handles.M2,'string'));
L1 = str2num(get(handles.L1,'string'));
L2 = str2num(get(handles.L2,'string'));
R1 = str2num(get(handles.R ,'string'));
R2 = str2num(get(handles.R2,'string'));
% figure('Name','FUN!')
if (isempty(numofplayers)||numofplayers==1)
anifunV3(theta,phi,psi, t,R1,R2, L1, L2,M1,M2,g)
else
    hitx=anifunV3_WAR(theta,phi,psi, t,R1,R2, L1, L2,M1,M2,g,p2,turn,height)
    if (turn==2)
        if (hitx>-height/2&&hitx<height/2)
            set(hObject,'string','2 wins!')
            '2 wins!!!'
            started=0;
        end
    else
        if (hitx>p2-height/2&&hitx<p2+height/2)
            set(hObject,'string','1 wins!')
            '1 wins!!!'
            started=0;
        end
    end
end
